package com.zhkj.shixi.tvplayer.MusicPlayer;

import android.app.Activity;
import android.content.DialogInterface;
import android.util.Log;

import com.zhkj.shixi.tvplayer.Interface.MusicMenuCtrl;
import com.zhkj.shixi.tvplayer.MyMediaPlayer;
import com.zhkj.shixi.tvplayer.PicturePlayer.CommonDialog;

import java.io.File;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.content.ContentValues.TAG;

/**
 * Created by suqiuyu on 19-8-8.
 */

public class MusicCtrl implements MusicMenuCtrl {
    private MyMediaPlayer mMediaPlayer;
    private Activity mActivity;
    private LrcView mLrcView;
    private Timer mTimer;

    public MusicCtrl(MyMediaPlayer mediaPlayer, Activity activity) {
        mMediaPlayer = mediaPlayer;
        mActivity = activity;
        mTimer = new Timer();
        //initPlay();
    }

    @Override
    public void play() {
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
        } else {
            mMediaPlayer.play();
        }
    }

    @Override
    public void pause() {
        mMediaPlayer.pause();
    }

    @Override
    public void prev() {
        File nowFile = ((AudioActivity) mActivity).getNowFile();
        List<File> list = ((AudioActivity) mActivity).getFileList();
        int index = list.indexOf(nowFile);
        if (index == 0) {
            CommonDialog.Builder builder =new CommonDialog.Builder(mActivity);
            builder.setMessage("没有上一个文件");
            builder.setTitle("提示");
            builder.setCenterTextView("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();


        } else {
            File newFile = list.get(index - 1);
            ((AudioActivity) mActivity).setNowFile(newFile);
            initPlay();
        }
    }

    @Override
    public void next() {
        File nowFile = ((AudioActivity) mActivity).getNowFile();
        List<File> list = ((AudioActivity) mActivity).getFileList();
        int index = list.indexOf(nowFile);
        if (index == (list.size() - 1)) {
            CommonDialog.Builder builder =new CommonDialog.Builder(mActivity);
            builder.setMessage("已经是最后一个文件了");
            builder.setTitle("提示");
            builder.setCenterTextView("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        } else {
            File newFile = list.get(index + 1);
            ((AudioActivity) mActivity).setNowFile(newFile);
            initPlay();
        }
    }

    @Override
    public void changePosition(int time) {
        mMediaPlayer.setTime(time * 1000);
    }

    @Override
    public void initPlay() {
        File nowFile = ((AudioActivity) mActivity).getNowFile();
        LrcController lrcController = new LrcController();
        mMediaPlayer.init(nowFile.getPath());
        Log.i(TAG, "initPlay: "+nowFile);
        File file =new File(nowFile.getParent()+"/"+nowFile.getName().substring(0,nowFile.getName().indexOf("."))+".lrc");
        if (file.exists()){
            lrcController.start(file,mActivity,mMediaPlayer);
            Log.w("LRC",file.getName());
        }else {
            Log.i(TAG, "initPlay: 暂无歌词");
        }
        //TODO 改变界面UI
    }

    public int getDuration() {
        return mMediaPlayer.getDuration();
    }

    public void upgradeSeekBar() {
        mTimer.cancel();
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((AudioActivity) mActivity).getAudioUI()
                                .setSeekBarProgress(mMediaPlayer.getCurrentPosition() / 1000);
                    }
                });
            }
        },0,300);
    }
}
